package com.lefeng.cockpit.datax.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefeng.cockpit.common.bean.dto.executor.ExecutorDTO;
import com.lefeng.cockpit.common.bean.po.executor.ExecutorPO;
import com.lefeng.cockpit.common.bean.vo.execute.ExecutorStateVO;
import com.lefeng.cockpit.common.bean.vo.execute.ExecutorVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author weifw
 */
@Mapper
public interface ExecutorMapper extends BaseMapper<ExecutorPO> {

    @Select("<script>" +
            "select " +
            "a.id, " +
            "a.app_name, " +
            "a.title, " +
            "a.address_type, " +
            "a.status, " +
            "a.create_by, " +
            "a.create_time, " +
            "a.update_by, " +
            "a.update_time, " +
            "b.registry_group, " +
            "b.registry_key, " +
            "b.registry_value, " +
            "b.cpu_usage, " +
            "b.memory_usage, " +
            "b.load_average, " +
            "b.py_path, " +
            "b.version " +
            "from " +
            "dc_datax_executor a " +
            "left join dc_datax_registry b on a.app_name = b.registry_key  " +
            "WHERE 1=1 " +
            "<if test='dto.searchKeywords != null'> AND a.app_name LIKE CONCAT('%', #{dto.searchKeywords}, '%') </if>" +
            "<if test='dto.statuses != null and dto.statuses.size() > 0'> " +
            "AND a.status IN " +
            "<foreach collection='dto.statuses' item='id' open='(' separator=',' close=')'> #{id} </foreach>" +
            "</if>" +
            "<choose>" +
            "   <when test='dto.sortField != null and dto.sortType != null'>" +
            "       ORDER BY ${dto.sortField} ${dto.sortType}" +
            "   </when>" +
            "   <otherwise>" +
            "       ORDER BY a.id ASC" +
            "   </otherwise>" +
            "</choose>" +
            "</script>")
    IPage<ExecutorVO> selectPageWithParams(Page<?> page, @Param("dto") ExecutorDTO executorDTO);


    @Select("SELECT " +
            "   SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS live_count, " +
            "   SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS down_count, " +
            "   COUNT(*) AS total " +
            "FROM dc_datax_executor")
    ExecutorStateVO getExecutorState();

}
